package com.liangzhong.modules.guide.mediator
{
	import com.common.utils.Reflection;
	import com.global.GlobalDef;
	import com.liangzhong.constants.ApplicationConstants;
	import com.liangzhong.constants.NotificationConstants;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.GuideEvent;
	import com.liangzhong.mediator.BaseMediator;
	import com.liangzhong.model.AppData;
	import com.liangzhong.proxy.login.LoginProxy;
	import com.lz.football.module.guide.IGuideSettingView;
	
	import flash.display.DisplayObject;
	
	import org.puremvc.as3.interfaces.INotification;

	/**
	 *新手设置 
	 * @author lake
	 * 
	 */	
	public class GuideSettingMediator extends BaseMediator
	{
		public static const MediatorName :String = "GuideSettingMediator";
		
		public function GuideSettingMediator(mediatorName:String=null, viewComponent:Object=null)
		{
			super(mediatorName, viewComponent);
		}
		/**
		 *获取视图 
		 * @return 
		 * 
		 */		
		public function get view():IGuideSettingView
		{
			return viewComponent as IGuideSettingView;
		}
		/**
		 *添加事件 
		 * 
		 */		
		override public function addEventListeners():void
		{
			super.addEventListeners();
			AppEventDispatcher.getInstance().addEventListener(GuideEvent.NAME_SELECT,selectNameHandler);
			AppEventDispatcher.getInstance().addEventListener(GuideEvent.COUNTRY_SELECT,selectCountryHandler);
			AppEventDispatcher.getInstance().addEventListener(GuideEvent.TEAMICON_SELECT,selectTeamIconHandler);
			AppEventDispatcher.getInstance().addEventListener(GuideEvent.PLACE_SELECT,selectPlaceHandler);
			AppEventDispatcher.getInstance().addEventListener(GuideEvent.QUK_CREATE_CLUB,qukCreateHandler);
		}
		/**
		 *删除事件 
		 * 
		 */		
		override public function removeEventListeners():void
		{
			super.removeEventListeners();
			AppEventDispatcher.getInstance().removeEventListener(GuideEvent.NAME_SELECT,selectNameHandler);
			AppEventDispatcher.getInstance().removeEventListener(GuideEvent.COUNTRY_SELECT,selectCountryHandler);
			AppEventDispatcher.getInstance().removeEventListener(GuideEvent.TEAMICON_SELECT,selectTeamIconHandler);
			AppEventDispatcher.getInstance().removeEventListener(GuideEvent.PLACE_SELECT,selectPlaceHandler);
			AppEventDispatcher.getInstance().removeEventListener(GuideEvent.QUK_CREATE_CLUB,qukCreateHandler);
		}
		/**
		 *名字选择 
		 * @param e
		 * 
		 */		
		private function selectNameHandler(e:GuideEvent):void
		{
			AppData.getInstance().guideManager.guideSettingVo.clubName = e.param;
			this.sendNotification(ApplicationConstants.GUIDE_CHECK_NAME,e.param);
			//view.showCountrySelectPanel();
		}
		/**
		 *国家选择 
		 * @param e
		 * 
		 */		
		private function selectCountryHandler(e:GuideEvent):void
		{
			AppData.getInstance().guideManager.guideSettingVo.country = e.param;
			view.showTeamIconSelectPanel();
		}
		/**
		 *队徽选择 
		 * @param e
		 * 
		 */		
		private function selectTeamIconHandler(e:GuideEvent):void
		{
			AppData.getInstance().guideManager.guideSettingVo.teamId = e.param;
			view.showPlaceSelectPanel();
		}
		/**
		 *位置选择 
		 * @param e
		 * 
		 */		
		private function selectPlaceHandler(e:GuideEvent):void
		{
			AppData.getInstance().guideManager.guideSettingVo.place = e.param;
			this.sendNotification(NotificationConstants.GUIDE_FINISH);
			this.sendNotification(ApplicationConstants.GUIDE_SETTING);
			removeMediator();
		}
		/**
		 *一键创建 
		 * @param e
		 * 
		 */		
		private function qukCreateHandler(e :GuideEvent):void
		{
			var countrys :Array = [2,4,5];
			var teamIds :Array = [1101,1102,1103,1104,1105,1106,1107,1108];
			var places :Array = [1,2,3];
			AppData.getInstance().guideManager.guideSettingVo.country = countrys[int(Math.random() * 3)];
			AppData.getInstance().guideManager.guideSettingVo.teamId = teamIds[int(Math.random() * 8)];
			AppData.getInstance().guideManager.guideSettingVo.place = places[int(Math.random() * 3)];
			this.sendNotification(NotificationConstants.GUIDE_FINISH);
			this.sendNotification(ApplicationConstants.GUIDE_SETTING);
			removeMediator();
		}
		/**
		 *删除mediator 
		 * 
		 */		
		override public function removeMediator():void
		{
			super.removeMediator();
			facade.removeMediator(MediatorName);
		}
		/**
		 *注册信息 
		 * @return 
		 * 
		 */		
		override public function listNotificationInterests():Array
		{
			return [LoginProxy.CheckUp_Name_Result];
		}
		/**
		 *捕获信息 
		 * @param notification
		 * 
		 */		
		override public function handleNotification(notification:INotification):void
		{
			var retCode:int ;
			switch(notification.getName())
			{
				case LoginProxy.CheckUp_Name_Result://名字验证
				{
					retCode = int(notification.getBody());
					if(retCode == 0)
					{
						view.showCountrySelectPanel();
					}
					break;
				}
			}
		}
		/**
		 * 注册
		 */		
		override public function onRegister():void
		{
			addEventListeners();
			viewComponent = Reflection.createInstance("com.lz.football.module.guide.setting.GuideSettingView");
			view.initUI();
			GlobalDef.GameWindowContainer.addChild(view as DisplayObject);
		}
		/**
		 * 删除
		 */		
		override public function onRemove():void
		{
			super.onRemove();
			removeEventListeners();
			view.destroyView();
		}
	}
}